package com.hqyj.onlineExamination.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.onlineExamination.modules.account.entity.Resource;
import com.hqyj.onlineExamination.modules.account.service.ResourceService;
import com.hqyj.onlineExamination.modules.common.entity.ResultEntity;
import com.hqyj.onlineExamination.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 127.0.0.1/api/resource ---- post
     * {"permission":"deleteUser"}
     */
    @PostMapping(value = "/resource", consumes = "application/json")
    public ResultEntity<Resource> insertResource(@RequestBody Resource resource) {
        return resourceService.insertResource(resource);
    }

    /**
     * 127.0.0.1/api/resource ---- put
     * {"id":"1","permission":"deleteUser"}
     */
    @PutMapping(value = "/resource", consumes = "application/json")
    public ResultEntity<Resource> updateResource(@RequestBody Resource resource) {
        return resourceService.updateResource(resource);
    }

    /**
     * 127.0.0.1/api/resource/2 ---- delete
     */
    @DeleteMapping("/resource/{id}")
    public ResultEntity<Object> deleteResourceById(@PathVariable int id) {
        return resourceService.deleteResourceById(id);
    }

    /**
     * 127.0.0.1/api/resource/1 ---- get
     */
    @GetMapping("/resource/{id}")
    public Resource getResourceById(@PathVariable int id) {
        return resourceService.getResourceById(id);
    }

    /**
     * 127.0.0.1/api/resources ---- post
     * {"currentPage":"1","pageSize":"5","orderBy":"id","direction":"desc","keyWord":""}
     */
    @PostMapping(value = "/resources", consumes = "application/json")
    public PageInfo<Resource> getResourcesBySearchBean(@RequestBody SearchBean searchBean) {
        return resourceService.getResourcesBySearchBean(searchBean);
    }
}
